package server;

	import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


	public class GringottsBankServer {
	
		public static void main(String[] args){
			ServerSocket atmServer=null;
			try {
				atmServer = new ServerSocket(5433);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			while (true) {
				try {
					Socket s1 = atmServer.accept();
					OutputStream s1out = s1.getOutputStream();
					BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
							s1out));
					
					bw.write("Hello Atm World!\n");
					bw.close();
					s1.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
		private boolean checkForID(BigDecimal bd){
			final String derbyProtocol = "jdbc:derby://localhost:1527/";
			final String dbName="GringottsATM";
			final String jdbcURL = derbyProtocol+dbName;
			final String derbyDriver = "org.apache.derby.jdbc.ClientDriver";
			final String query="SELECT * from users";
			final String driver = derbyDriver;
			BigDecimal bInt=null;
			System.setProperty("jdbc.drivers", driver);
			try {
				Connection conn = (Connection) DriverManager.getConnection(jdbcURL, "app","root");
				Statement statement = null;
				ResultSet rs = null;
				try {
				statement = conn.createStatement();
				rs=statement.executeQuery(query);
				while (rs.next()) {
					 bInt=rs.getBigDecimal("USER_ID");
				System.out.println("Name: "+rs.getString("USER_ID"));	
				
				}
				}
				finally {
				if (rs!=null) rs.close();
				if (conn!=null) conn.close();}}
				catch (SQLException se) {System.out.println("SQL Error: "+se);}
			return  bInt.equals(bd);

		}
	}

